package com.chen.controller.admin;

import com.alibaba.fastjson.JSON;
import com.chen.pojo.Vip;
import com.chen.service.vip.VipService;
import com.chen.util.DataGridViewResult;
import com.chen.util.SystemConstrant;
import com.chen.vo.VipVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/vip")
public class VipController {
    @Autowired
    @Qualifier("VipServiceImpl")
    private VipService vipService;

    /**
     * 登录后台
     * @param vipNo
     * @param vipPassword
     * @param session
     * @return
     */
    @RequestMapping(value = "/login",produces = {"application/json;charset=UTF-8"})
    public String login(String vipNo, String vipPassword, HttpSession session){

        Map<String,Object> map = new HashMap<String, Object>();
        Vip vip = vipService.login(vipNo, vipPassword);
        //判断对象是否为空，不为空表示登录成功
        if (vip != null){
            //保存当前登录用户
            session.setAttribute(SystemConstrant.LOGINUSER,vip);
            map.put(SystemConstrant.SUCCESS,true);//成功
        }else{
            map.put(SystemConstrant.SUCCESS,false);//失败
            map.put(SystemConstrant.MESSAGE,"账号密码错误，登录失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 员工列表
     * @param vipVo
     * @return
     */
    @RequestMapping(value = "/list",produces = {"application/json;charset=UTF-8"})
    public DataGridViewResult list(VipVo vipVo){
        //设置分页信息
        PageHelper.startPage(vipVo.getPage(),vipVo.getLimit());
        //调用查询的方法
        List<Vip> employeeList = vipService.findEmployeeList(vipVo);
        //创建分页对象
        PageInfo<Vip> pageInfo = new PageInfo<Vip>(employeeList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加员工
     * @param vip
     * @param session
     * @return
     */
    @RequestMapping(value = "/addEmployee",produces = {"application/json;charset=UTF-8"})
    public String addEmployee(Vip vip,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Vip loginUser = (Vip) session.getAttribute(SystemConstrant.LOGINUSER);
        //设置创建人
        vip.setCreatedBy(loginUser.getVipId());
        //调用新增员工的方法
        if(vipService.addEmployee(vip)>0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "添加成功");
        }else{
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "添加失败");
        }

        return JSON.toJSONString(map);
    }

    /**
     * 修改员工
     * @param vip
     * @return
     */
    @RequestMapping(value = "/updateEmployee",produces = {"application/json;charset=UTF-8"})
    public String updateEmployee(Vip vip,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Vip loginUser = (Vip) session.getAttribute(SystemConstrant.LOGINUSER);
        //设置创建人
        vip.setCreatedBy(loginUser.getVipId());
        //调用新增员工的方法
        if(vipService.updateEmployee(vip)>0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "修改成功");
        }else{
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除员工
     * @param vipId
     * @return
     */
    @RequestMapping(value = "/deleteById",produces = {"application/json;charset=UTF-8"})
    public String deleteById(int vipId) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除方法
        if (vipService.deleteById(vipId) > 0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "删除成功");
        } else {
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 删除员工
     * @param vipId
     * @return
     */
    @RequestMapping(value = "/resetPwd",produces = {"application/json;charset=UTF-8"})
    public String resetPwd(int vipId) {
        Map<String, Object> map = new HashMap<String, Object>();
        //调用删除方法
        if (vipService.resetPwd(vipId) > 0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "重置密码成功");
        } else {
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "重置密码失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 分配角色
     * @param roleIds
     * @param empId
     * @return
     */
    @RequestMapping(value = "/saveEmployeeRole",produces = {"application/json;charset=UTF-8"})
    public String saveEmployeeRole(String roleIds,Integer empId){
        Map<String,Object> map = new HashMap<String,Object>();
        //调用保存员工角色关系的方法
        if(vipService.saveEmployeeRole(roleIds,empId)){
            map.put(SystemConstrant.MESSAGE,"角色分配成功");
        }else{
            map.put(SystemConstrant.MESSAGE,"角色分配失败");
        }
        return JSON.toJSONString(map);
    }


    /**
     * 会员列表
     * @param vipVo
     * @return
     */
    @RequestMapping(value = "/findVipList",produces = {"application/json;charset=UTF-8"})
    public DataGridViewResult findVipList(VipVo vipVo){
        //设置分页信息
        PageHelper.startPage(vipVo.getPage(),vipVo.getLimit());
        //调用查询的方法
        List<Vip> employeeList = vipService.findVipList(vipVo);
        //创建分页对象
        PageInfo<Vip> pageInfo = new PageInfo<Vip>(employeeList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 添加会员
     * @param vip
     * @param session
     * @return
     */
    @RequestMapping(value = "/addVipHo",produces = {"application/json;charset=UTF-8"})
    public String addVipHo(Vip vip,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Vip loginUser = (Vip) session.getAttribute(SystemConstrant.LOGINUSER);
        //设置创建人
        vip.setCreatedBy(loginUser.getVipId());
        //调用新增员工的方法
        if(vipService.addVipHo(vip)>0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "添加成功");
        }else{
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 修改会员
     * @param vip
     * @return
     */
    @RequestMapping(value = "/updateVip",produces = {"application/json;charset=UTF-8"})
    public String updateVip(Vip vip,HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户
        Vip loginUser = (Vip) session.getAttribute(SystemConstrant.LOGINUSER);
        //设置创建人
        vip.setCreatedBy(loginUser.getVipId());
        //调用新增员工的方法
        if(vipService.updateVip(vip)>0) {
            map.put(SystemConstrant.SUCCESS, true);
            map.put(SystemConstrant.MESSAGE, "修改成功");
        }else{
            map.put(SystemConstrant.SUCCESS, false);
            map.put(SystemConstrant.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }
}
